package project.ccso.top.dao.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import project.ccso.top.dao.ThreeCategoryDao;
import project.ccso.top.entity.ThreeCategory;
import project.ccso.top.util.DateUtil;

/**
 * 三级分类Dao的实现类
 * @author aibinxiao
 * @date 2018年8月23日 上午10:42:53
 */
public class ThreeCategoryDaoImpl implements ThreeCategoryDao {
	// 日志
	private static Logger logger = LoggerFactory.getLogger(ThreeCategoryDaoImpl.class);

	/*// 1.根据二级商品分类Id获取启用的三级商品分类集合Dao实现类
	@Override
	public List<ThreeCategory> findThreeCategoryByTwoId(Integer twoCategoryId) {
		String hql = "from ThreeCategory where twoCategoryId = ? and status = ?";
		List<ThreeCategory> result = null;
		List<ThreeCategory> list = this.getHibernateTemplate().find(hql, twoCategoryId, 0);
		if(list != null && list.size() > 0){
			logger.info("所有三级商品分类集合：" + list);
			result = list;
		}
		return result;
	}

	// 2.分页条件查询指定一级分类和二级分类下的三级分类记录数量Dao实现类
	@Override
	public int countByParams(String id, String name, String code, String startDate, String endDate,
			String oneCategoryIdStr, String twoCategoryIdStr) {
		int count = 0;
		// 使用离线条件查询.
		DetachedCriteria criteria = DetachedCriteria.forClass(ThreeCategory.class);
		if(!StringUtils.isEmpty(oneCategoryIdStr)){
			Integer oneCategoryId = Integer.valueOf(oneCategoryIdStr);
			criteria.add(Restrictions.eq("oneCategoryId", oneCategoryId));
		}
		if(!StringUtils.isEmpty(twoCategoryIdStr)){
			Integer twoCategoryId = Integer.valueOf(twoCategoryIdStr);
			criteria.add(Restrictions.eq("twoCategoryId", twoCategoryId));
		}
		if(!StringUtils.isEmpty(id)){
			Integer threeCategoryId = Integer.valueOf(id);
			criteria.add(Restrictions.idEq(threeCategoryId));
		}
		if(!StringUtils.isEmpty(name)){
			criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(code)){
			criteria.add(Restrictions.like("code", code, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			criteria.add(Restrictions.between("createTime", startDate, endDate));
		}
		// 倒序排序输出:
		criteria.addOrder(Order.asc("id"));
		// 执行查询:
		List<ThreeCategory> list = this.getHibernateTemplate().findByCriteria(criteria);
		if (list != null && list.size() > 0) {
			count = list.size();
			logger.info("找到("+ list.size() +")个符合查询条件的三级级商品分类");
		} else{
			logger.info("没有找到符合查询条件求的三级级商品分类");
		}
		return count;
	}

	// 3.分页条件查询指定一级分类和二级分类下的三级分类记录集合Dao实现类
	@Override
	public List<ThreeCategory> findPageByParams(String id, String name, String code, String startDate, String endDate,
			String oneCategoryIdStr, String twoCategoryIdStr, int startIndex, int records) {
		List<ThreeCategory> result = null;
		// 使用离线条件查询.
		DetachedCriteria criteria = DetachedCriteria.forClass(ThreeCategory.class);
		if(!StringUtils.isEmpty(oneCategoryIdStr)){
			Integer oneCategoryId = Integer.valueOf(oneCategoryIdStr);
			criteria.add(Restrictions.eq("oneCategoryId", oneCategoryId));
		}
		if(!StringUtils.isEmpty(oneCategoryIdStr)){
			Integer twoCategoryId = Integer.valueOf(twoCategoryIdStr);
			criteria.add(Restrictions.eq("twoCategoryId", twoCategoryId));
		}
		if(!StringUtils.isEmpty(id)){
			Integer threeCategoryId = Integer.valueOf(id);
			criteria.add(Restrictions.idEq(threeCategoryId));
		}
		if(!StringUtils.isEmpty(name)){
			criteria.add(Restrictions.like("name", name, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(code)){
			criteria.add(Restrictions.like("code", code, MatchMode.ANYWHERE));
		}
		if(!StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate)){
			startDate += " 00:00:00";
			endDate += " 23:59:59";
			criteria.add(Restrictions.between("createTime", startDate, endDate));
		}
		// 倒序排序输出:
		criteria.addOrder(Order.asc("id"));
		// 执行查询:
		List<ThreeCategory> list = this.getHibernateTemplate().findByCriteria(criteria, startIndex, records);
		if (list != null && list.size() > 0) {
			result = list;
			logger.info("找到("+ list.size() +")个符合查询条件的三级级商品分类");
		} else{
			logger.info("没有找到符合查询条件求的三级级商品分类");
		}
		return result;
	}

	// 4.根据ID查询三级分类Dao实现类
	@Override
	public ThreeCategory findById(Integer id) {
		String hql = "from ThreeCategory where id = ?";
		ThreeCategory result = null;
		List<ThreeCategory> list = this.getHibernateTemplate().find(hql, id);
		if(list != null && list.size() > 0){
			logger.info("根据ID:"+id+",查询到三级分类:" + list.get(0));
			result = list.get(0);
		}
		return result;
	}

	// 5.保存或者更新三级分类信息Dao实现类
	@Override
	public void saveThreeCategory(ThreeCategory threeCategory) throws Exception {
		this.getHibernateTemplate().saveOrUpdate(threeCategory);
	}

	// 6.更新三级分类信息状态
	@Override
	public void updateStatus(Integer id, Integer statusCode) throws Exception {
		String hql = "from ThreeCategory where id = ?";
		List<ThreeCategory> list = this.getHibernateTemplate().find(hql, id);
		if(list!=null && list.size()>0){
			ThreeCategory threeCategoty = list.get(0);
			threeCategoty.setStatus(statusCode);
			threeCategoty.setAlterTime(DateUtils.DateToStr(new Date()));
			this.getHibernateTemplate().saveOrUpdate(threeCategoty);
		}
	}
	
	// 7.根据二级商品分类Id获取启用的三级商品分类集合Dao实现类
	@Override
	public List<ThreeCategory> findAllThreeCategoryByTwoId(Integer twoCategoryId) {
		String hql = "from ThreeCategory where twoCategoryId = ?";
		List<ThreeCategory> result = null;
		List<ThreeCategory> list = this.getHibernateTemplate().find(hql, twoCategoryId);
		if(list != null && list.size() > 0){
			logger.info("所有三级商品分类集合：" + list);
			result = list;
		}
		return result;
	}*/
}
